"""
@File    :   route_depend.py
@Time    :   2024/03/28 23:35:28
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   路由依赖(用于维持本业务的路由部分整洁)
"""

from typing import Annotated

from fastapi import Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession

from src.business.admin.system.oauth2.token.dependencies.no_introduction import (
    GetTenantIdFromAccessTokenDependencies,
    GetUserNameFromAccessTokenDependencies,
)
from src.business.admin.system.role.schemas.out import RoleCompleteOutSchema
from src.business.admin.system.role.schemas.validate import (
    RoleAssignMenuPermissionValidateSchema,
    RoleCreateValidateSchema,
    RoleDeleteValidateSchema,
    RoleListSearchValidateSchema,
    RoleUpdateStatusValidateSchema,
    RoleUpdateValidateSchema,
)
from src.business.admin.system.role.service import RoleService
from src.dependencies.no_introduction import depend_get_master_db_session
from src.schemas.validate import (
    OrderBySearchValidateSchema,
    PaginationSearchValidateSchema,
)


async def route_depend_get_role(
    role_id: Annotated[int, Annotated[int, Query(..., ge=1, description="角色ID")]],
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> RoleCompleteOutSchema:
    """查询单个角色的路由依赖

    Args:
        role_id (Annotated[int, Depends): 角色ID
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        RoleCompleteOutSchema: 角色信息
    """
    role: RoleCompleteOutSchema = await RoleService.get_role_use_id(
        db_session,
        tenant_id,
        role_id,
        raise_disabled_exception=False,
    )
    return role


async def route_depend_list_role(
    search_schema: RoleListSearchValidateSchema,
    pagination_schema: PaginationSearchValidateSchema,
    order_schema: OrderBySearchValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> tuple[list[RoleCompleteOutSchema], int]:
    """查询角色列表的路由依赖

    Args:
        search_schema (Annotated[TenantListSearchValidateSchema, Depends): 查询参数
        pagination_schema (Annotated[PaginationSearchValidateSchema, Depends): 分页参数
        order_schema (Annotated[OrderBySearchValidateSchema, Depends): 排序参数
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        tuple[list[TenantCompleteOutSchema], int]: 查询结果
    """
    # 注解
    search_pagination_result: list[RoleCompleteOutSchema]
    search_total: int
    # 列表结果查询
    (
        search_pagination_result,
        search_total,
    ) = await RoleService.list_search_role(
        db_session, tenant_id, search_schema, pagination_schema, order_schema
    )
    return search_pagination_result, search_total


async def route_depend_create_role(
    create_schema: RoleCreateValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> int:
    """创建角色的依赖

    Args:
        create_schema (UserCreateValidateSchema): 创建所需的参数
        tenant_id: 当前token指向的租户ID
        username: 当前token指向的角色名
        db_session: 数据库连接池

    Returns:
        int: 创建的sql的主键ID
    """
    # 创建角色
    created_id = await RoleService.create_role(
        db_session, tenant_id, create_schema, creator_username=username
    )
    return created_id


async def route_depend_update_role(
    update_schema: RoleUpdateValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """修改角色的路由依赖

    Args:
        update_schema (TenantUpdateValidateSchema): 修改所需的参数
        tenant_id: 当前token指向的租户ID
        username: 当前token指向的角色名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await RoleService.update_role(
        db_session, tenant_id, update_schema, updater_username=username
    )


async def route_depend_update_role_status(
    update_schema: RoleUpdateStatusValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """单独修改角色状态的路由依赖

    Args:
        update_schema (TenantUpdateStatusValidateSchema): 修改所需的参数
        tenant_id: 当前token指向的租户ID
        username: 当前token指向的角色名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await RoleService.update_role_status(
        db_session, tenant_id, update_schema, updater_username=username
    )


async def route_depend_delete_role(
    delete_schema: RoleDeleteValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """删除角色的路由依赖

    Args:
        delete_schema (TenantDeleteValidateSchema): 删除所需的参数
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 删除操作
    await RoleService.delete_role(db_session, tenant_id, delete_schema)


async def route_depend_assign_role_menu_permission(
    assign_schema: RoleAssignMenuPermissionValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """分配角色菜单权限的路由依赖

    Args:
        assign_schema: 分配操作所需的参数
        tenant_id (Annotated[ int, Depends): 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 分配操作
    await RoleService.assign_role_menu_permission(db_session, tenant_id, assign_schema)
